package tp_imag.model;

import ij.*;
import ij.plugin.filter.PlugInFilter;
import ij.process.*;
import ij.ImagePlus;
import ij.gui.*;
import java.awt.*;



public class Sobel_ extends Filter implements PlugInFilter {

	  /**
     * The gaussian kernel.
     */
    private float[] gaussianKernel = null;
	
    @Override
    public float[] getParameters() {
        return null;
    }

    @Override
    public String getName() {
        return "Sobel";
    }

    @Override
    public String toString() {
        return "Filtro: " + getName();
    }

	public int setup(String arg, ImagePlus imp) {
		return DOES_8G;
	}

	public void run(ImageProcessor ip) {
        final int MAX_LENGTH = 1000;
        int w = ip.getWidth();
        int h = ip.getHeight();
        int sum;
        int[][] dx={
			{-1, -2, -1},
			{0,  0, 0},
			{1,2,1}
		};

        ImagePlus zx = NewImage.createByteImage ("gx image", w, h, 1, NewImage.FILL_BLACK);
		ImageProcessor gx = zx.getProcessor();

           for (int v=1; v<=h-2; v++) {
	             for (int u=1; u<=w-2; u++) {

	               sum=0;
	                 for (int j=-1; j<=1; j++) {
	                     for (int i=-1; i<=1; i++) {
	                         sum += ip.getPixel(u+i,v+j) * dx[i+1][j+1];

	                     }
	                 }
                     sum = Math.abs(sum);
	                 gx.putPixel(u,v,sum);
	             }
        }
        zx.show();

  int[][] dy={
			{-1, 0, 1},
			{-2,  0, 2},
			{-1,0,1}
		};

        ImagePlus zy = NewImage.createByteImage ("gy image", w, h, 1, NewImage.FILL_BLACK);
		ImageProcessor gy = zy.getProcessor();

           for (int v=1; v<=h-2; v++) {
	             for (int u=1; u<=w-2; u++) {

	               sum=0;
	                 for (int j=-1; j<=1; j++) {
	                     for (int i=-1; i<=1; i++) {
	                         sum += ip.getPixel(u+i,v+j) * dy[i+1][j+1];

	                     }
	                 }
                     sum = Math.abs(sum);
	                 gy.putPixel(u,v,sum);
	             }
        }
        zy.show();

        // the magnitude of ip is gm= abs(gx)+abs(gy)
        ImagePlus zm = NewImage.createByteImage ("The magnitude image", w, h, 1, NewImage.FILL_BLACK);
		ImageProcessor gm = zm.getProcessor();
		 for (int v=1; v<=h-2; v++) {
			             for (int u=1; u<=w-2; u++) {

			              sum=gx.getPixel(u,v) + gy.getPixel(u,v);
			                 gm.putPixel(u,v,sum);
			             }
        }
		zm.show();

		// the edge of ip is ge= 255 if gm > threshold else ge=0
		        ImagePlus ze = NewImage.createByteImage ("The edge image", w, h, 1, NewImage.FILL_BLACK);
				ImageProcessor ge = ze.getProcessor();
				 for (int v=1; v<=h-2; v++) {
					             for (int u=1; u<=w-2; u++) {
		                             if ( gm.getPixel(u,v) > 100 ) ge.putPixel(u,v,255); // threshold=100
		                             else ge.putPixel(u,v,0);

					             }
		        }
				ze.show();

   }
	
	public ImagePlus Sobel(ImageProcessor ip) {
		
		
		   final int MAX_LENGTH = 1000;
	        int w = ip.getWidth();
	        int h = ip.getHeight();
	        int sum;
	        int[][] dx={
				{-1, -2, -1},
				{0,  0, 0},
				{1,2,1}
			};

	        ImagePlus zx = NewImage.createByteImage ("gx image", w, h, 1, NewImage.FILL_BLACK);
			ImageProcessor gx = zx.getProcessor();

	           for (int v=1; v<=h-2; v++) {
		             for (int u=1; u<=w-2; u++) {

		               sum=0;
		                 for (int j=-1; j<=1; j++) {
		                     for (int i=-1; i<=1; i++) {
		                         sum += ip.getPixel(u+i,v+j) * dx[i+1][j+1];

		                     }
		                 }
	                     sum = Math.abs(sum);
		                 gx.putPixel(u,v,sum);
		             }
	        }
	       

	  int[][] dy={
				{-1, 0, 1},
				{-2,  0, 2},
				{-1,0,1}
			};

	        ImagePlus zy = NewImage.createByteImage ("gy image", w, h, 1, NewImage.FILL_BLACK);
			ImageProcessor gy = zy.getProcessor();

	           for (int v=1; v<=h-2; v++) {
		             for (int u=1; u<=w-2; u++) {

		               sum=0;
		                 for (int j=-1; j<=1; j++) {
		                     for (int i=-1; i<=1; i++) {
		                         sum += ip.getPixel(u+i,v+j) * dy[i+1][j+1];

		                     }
		                 }
	                     sum = Math.abs(sum);
		                 gy.putPixel(u,v,sum);
		             }
	        }
	       

	        // the magnitude of ip is gm= abs(gx)+abs(gy)
	        ImagePlus zm = NewImage.createByteImage ("The magnitude image", w, h, 1, NewImage.FILL_BLACK);
			ImageProcessor gm = zm.getProcessor();
			 for (int v=1; v<=h-2; v++) {
				             for (int u=1; u<=w-2; u++) {

				              sum=gx.getPixel(u,v) + gy.getPixel(u,v);
				                 gm.putPixel(u,v,sum);
				             }
	        }
		

			// the edge of ip is ge= 255 if gm > threshold else ge=0
			        ImagePlus ze = NewImage.createByteImage ("The edge image", w, h, 1, NewImage.FILL_BLACK);
					ImageProcessor ge = ze.getProcessor();
					 for (int v=1; v<=h-2; v++) {
						             for (int u=1; u<=w-2; u++) {
			                             if ( gm.getPixel(u,v) > 100 ) ge.putPixel(u,v,255); // threshold=100
			                             else ge.putPixel(u,v,0);

						             }
			        }
					return ze;
		
		
	}
}

